package br.com.uniban.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import br.com.uniban.bo.QuestaoBO;
import br.com.uniban.bo.UsuarioBO;
import br.com.uniban.entity.Disciplina;
import br.com.uniban.entity.Usuario;
import br.com.uniban.form.UsuarioForm;

public class UsuarioAction extends DispatchAction {

	private UsuarioBO bo = null;
	public static final String USUARIO_FORM = "UsuarioForm";
	private static final String LOGAR = "logar";
	private static final String DESLOGAR = "deslogar";
	private static final String LOGADO = "logado";
	public static final String LISTA_DISCIPLINA = "listaDisciplina";
	
	public UsuarioAction(){
		try {
			if (bo == null)
				bo = new UsuarioBO();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ActionForward logar(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)throws Exception {

		UsuarioForm form = (UsuarioForm) actionForm;
		Usuario usuario = form.getUsuario();
		usuario = bo.logar(usuario);

		if (usuario != null) {
			form.setUsuario(usuario);
			if(request.getSession().getAttribute(LISTA_DISCIPLINA) == null){
				QuestaoBO questaoBO = new QuestaoBO();
				List<Disciplina> listDisciplinas = questaoBO.listaDisciplina();
				request.getSession().setAttribute(LISTA_DISCIPLINA, listDisciplinas);
			}
			return mapping.findForward(LOGADO);
		} else {
			response.getWriter().write(Boolean.FALSE.toString());
			return null;
		}

	}
	
	public ActionForward deslogar(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)throws Exception {

		request.getSession().invalidate();
		return mapping.findForward(DESLOGAR);

	}

	public ActionForward cadastrar(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		UsuarioForm form = (UsuarioForm) actionForm;
		String erros = bo.cadastaUsuario(form.getUsuario());
		
		if(erros != null){
			response.setStatus(response.SC_BAD_REQUEST);
			response.getWriter().write(erros);
		}
		return null;
	}

	public ActionForward atualizar(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		UsuarioForm form = (UsuarioForm) actionForm;
		String erros = bo.alteraUsuario(form.getUsuario());
		
		if(erros != null){
			response.setStatus(response.SC_BAD_REQUEST);
			response.getWriter().write(erros);
		}
		return null;

	}
	
	public ActionForward recupLogin(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		UsuarioForm form = (UsuarioForm) actionForm;
		
		Usuario usuario = form.getUsuario();
		
		usuario.setCPF(request.getParameter("CPF"));
		usuario.setEmail(request.getParameter("userEmail"));
		usuario.getLogin().setLogin(request.getParameter("userEmail"));
		usuario.getLogin().setPerguntaSecreta(request.getParameter("userPS"));
		
		Boolean isSucesso = bo.recuperarLogin(usuario);
		response.getWriter().write(isSucesso.toString());
		return null;

	}
	
	public static Usuario getUsuario (HttpServletRequest request){
		UsuarioForm usuarioForm = (UsuarioForm) request.getSession().getAttribute(UsuarioAction.USUARIO_FORM);
		return usuarioForm.getUsuario();
	}
}
